#! /usr/bin/perl

@rows = map { [split /,/] } <DATA>;
$nrows = $#rows;
push @cost, $rows[$_][0] for (0..$nrows);

for $x (1..$nrows) {
  for $y (0..$nrows) {
    $mm = min_n($cost[$y], $cost[$y-1]);

    $cost[$y] = min_n($cost[$y], $cost[$y-1]) + $rows[$y][$x];
  }
  for $y (reverse (0..$nrows-2)) {
    $mm = min_n($cost[$y], $cost[$y+1]);
    $cost[$y] = min_n($cost[$y], $cost[$y+1] + $rows[$y][$x]);
  }
  print "\n@cost\n";
}

print min_n(@cost);
 
sub min_n { return (sort {$a<=>$b} grep {defined} @_)[0] }
__DATA__
131, 673, 234, 103, 18 
201, 96, 342, 965, 150 
630, 803, 746, 422, 111 
537, 699, 497, 121, 956 
805, 732, 524, 37, 331
